package com.ly.heart.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ly.common.core.domain.AjaxResult;
import com.ly.common.exception.CustomException;
import com.ly.common.utils.DateUtils;
import com.ly.common.utils.SecurityUtils;
import com.ly.common.utils.StringUtils;
import com.ly.common.utils.erp.ReturnPage;
import com.ly.common.utils.erp.SnowflakeIdWorker;
import com.ly.heart.domain.KingBaseTaxRateCategoryEntity;
import com.ly.heart.mapper.KingBaseTaxRateCategoryMapper;
import com.ly.heart.service.IKingBaseTaxRateCategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 金蝶-税率-税种 服务实现类
 * </p>
 *
 * @author WuZH
 * @since 2024-08-20
 */
@Service
public class KingBaseTaxRateCategoryServiceImpl extends ServiceImpl<KingBaseTaxRateCategoryMapper, KingBaseTaxRateCategoryEntity> implements IKingBaseTaxRateCategoryService {

    @Resource
    private KingBaseTaxRateCategoryMapper kingBaseTaxRateCategoryMapper;

    @Override
    public AjaxResult getKingBaseTaxRateCategoryList ( KingBaseTaxRateCategoryEntity kingBaseTaxRateCategoryEntity ) {
        List<KingBaseTaxRateCategoryEntity> list = kingBaseTaxRateCategoryMapper.selectKingBaseTaxRateCategoryEntityList ( kingBaseTaxRateCategoryEntity );
        Page<KingBaseTaxRateCategoryEntity> page = new Page<> ( kingBaseTaxRateCategoryEntity.getPage (), kingBaseTaxRateCategoryEntity.getLimit () );
        return AjaxResult.success ( ReturnPage.getReturnPage ( page, list ) );
    }

    /**
     * 查询金蝶-税率-税种
     *
     * @param id 金蝶-税率-税种ID
     * @return 金蝶-税率-税种
     */
    @Override
    public KingBaseTaxRateCategoryEntity selectKingBaseTaxRateCategoryEntityById ( String id ) {
        return kingBaseTaxRateCategoryMapper.selectKingBaseTaxRateCategoryEntityById ( id );
    }

    /**
     * 查询金蝶-税率-税种列表
     *
     * @param kingBaseTaxRateCategoryEntity 金蝶-税率-税种
     * @return 金蝶-税率-税种
     */
    @Override
    public List<KingBaseTaxRateCategoryEntity> selectKingBaseTaxRateCategoryEntityList ( KingBaseTaxRateCategoryEntity kingBaseTaxRateCategoryEntity ) {
        return kingBaseTaxRateCategoryMapper.selectKingBaseTaxRateCategoryEntityList ( kingBaseTaxRateCategoryEntity );
    }

    /**
     * 新增金蝶-税率-税种
     *
     * @param kingBaseTaxRateCategoryEntity 金蝶-税率-税种
     * @return 结果
     */
    @Override
    public int insertKingBaseTaxRateCategoryEntity ( KingBaseTaxRateCategoryEntity kingBaseTaxRateCategoryEntity ) {
        kingBaseTaxRateCategoryEntity.setId ( SnowflakeIdWorker.getInstance ().nextStringId () );
        if ( StringUtils.isEmpty ( kingBaseTaxRateCategoryEntity.getDocNo () ) ) {
            int count = this.count ();
            kingBaseTaxRateCategoryEntity.setDocNo ( "SZ".concat ( String.format ( "%08d", count ) ) );
        }
        kingBaseTaxRateCategoryEntity.setCreateBy ( SecurityUtils.getLoginUser ().getUser ().getUserId () );
        kingBaseTaxRateCategoryEntity.setCreateTime ( DateUtils.getNowDate () );
        boolean save = this.save ( kingBaseTaxRateCategoryEntity );
        return save ? 1 : 0;
    }

    /**
     * 修改金蝶-税率-税种
     *
     * @param kingBaseTaxRateCategoryEntity 金蝶-税率-税种
     * @return 结果
     */
    @Override
    public int updateKingBaseTaxRateCategoryEntity ( KingBaseTaxRateCategoryEntity kingBaseTaxRateCategoryEntity ) {
        kingBaseTaxRateCategoryEntity.setUpdateBy ( SecurityUtils.getLoginUser ().getUser ().getUserId () );
        kingBaseTaxRateCategoryEntity.setUpdateTime ( DateUtils.getNowDate () );
        boolean b = this.updateById ( kingBaseTaxRateCategoryEntity );
        return b ? 1 : 0;
    }

    /**
     * 批量删除金蝶-税率-税种
     *
     * @param ids 需要删除的金蝶-税率-税种ID
     * @return 结果
     */
    @Override
    public int deleteKingBaseTaxRateCategoryEntityByIds ( String[] ids ) {
        return kingBaseTaxRateCategoryMapper.deleteKingBaseTaxRateCategoryEntityByIds ( ids );
    }

    /**
     * 删除金蝶-税率-税种信息
     *
     * @param id 金蝶-税率-税种ID
     * @return 结果
     */
    @Override
    public int deleteKingBaseTaxRateCategoryEntityById ( String id ) {
        return kingBaseTaxRateCategoryMapper.deleteKingBaseTaxRateCategoryEntityById ( id );
    }

    @Override
    public AjaxResult disabled ( String id ) {
        KingBaseTaxRateCategoryEntity kingBaseTaxRateCategoryEntity = this.getById ( id );
        if ( kingBaseTaxRateCategoryEntity == null ) {
            throw new CustomException ( "无此税种！" );
        }
        boolean update = this.update ( new LambdaUpdateWrapper<KingBaseTaxRateCategoryEntity> ().
                set ( KingBaseTaxRateCategoryEntity :: getIsDisabled, kingBaseTaxRateCategoryEntity.getIsDisabled () == 1 ? 2 : 1 ).
                set ( KingBaseTaxRateCategoryEntity :: getUpdateBy, SecurityUtils.getLoginUser ().getUser ().getUserId () ).
                set ( KingBaseTaxRateCategoryEntity :: getUpdateTime, DateUtils.getNowDate () ).
                eq ( KingBaseTaxRateCategoryEntity :: getId, id ) );
        if ( ! update ) {
            throw new CustomException ( kingBaseTaxRateCategoryEntity.getIsDisabled () == 1 ? "禁用税种失败！" : "解禁税种失败" );
        }
        return AjaxResult.success ();
    }

}
